/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

let id = 0;
let moduleIndex = 0;
const moduleIndices: Map<string, number> = new Map<string, number>();

/**
 * 生成唯一的id
 *
 * @since 2022/8/12
 */
export function generateId(): string {
  return `${id++}`;
}

export function getModuleIndex(modulePath: string, isWidget = false): number {
  const key = `${modulePath}:${isWidget}`;
  if (!moduleIndices.has(key)) {
    moduleIndices.set(key, moduleIndex++);
  }

  // Safety: 上面已判断modulePath是否存在
  return moduleIndices.get(key)!;
}

export function getWorkerIdWithModule(modulePath: string, isWidget = false): number | undefined {
  const key = `${modulePath}:${isWidget}`;
  return moduleIndices.get(key);
}

export function setWorkerIdWithModule(modulePath: string, workerId: number, isWidget = false): void {
  const key = `${modulePath}:${isWidget}`;
  moduleIndices.set(key, workerId);
}